package com.example.flashsales.service.impl;

import com.example.flashsales.entity.dto.RefundDetailsDto;
import com.example.flashsales.entity.dto.RefundDto;
import com.example.flashsales.entity.dto.SortDto;
import com.example.flashsales.entity.pojo.OrderDetails;
import com.example.flashsales.entity.pojo.Sorting;
import com.example.flashsales.entity.pojo.SortingDetails;
import com.example.flashsales.entity.vo.*;
import com.example.flashsales.mappers.OrderMapper;
import com.example.flashsales.mappers.SortMapper;
import com.example.flashsales.service.OrderDetailsService;
import com.example.flashsales.service.SortService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


@Service
public class SortServiceImpl implements SortService {

    @Autowired
    SortMapper sortMapper;

    @Autowired
    OrderServiceImpl orderService;

    @Autowired
    OrderDetailsService orderDetailsService;

    @Autowired
    AdminServiceImpl adminService;

    /**
     *
     * 查看订单细节与重量相近的（按重量排序）@param countm
     * @param goodsId
     * @return
     */
    @Override
    public List<OrderDetails> viewOrderDetailsVo(double count,long goodsId) {
        final List<OrderDetails> orderDetails = orderDetailsService.viewDetails(goodsId,count);
        orderDetails.sort(new Comparator<OrderDetails>() {
            @Override
            public int compare(OrderDetails o1, OrderDetails o2) {
                return (int) ((int) (o1.getXCount()-count) - (o2.getXCount()-count));
            }
        });
        return orderDetails;
    }

    /**
     * 修改订单细节状态
     * @param detailsId
     * @return
     */
    @Override
    @Transactional
    public boolean updateOrderDetailsState(long detailsId,int adminId,long stockId,Double count) {
        final OrderDetails orderDetails = orderDetailsService.viewOrderDetails(detailsId);
        if (orderDetails == null){
            return false;
        }
        //TODO 判断是否要退款
        if (count < orderDetails.getXCount()){
            //TODO 退款
            RefundDto refundDto = new RefundDto();
            refundDto.setAdminId(adminId);
            refundDto.setOrderId(orderDetails.getOrderId());
            refundDto.setReturnWhy("部分退款");
            refundDto.setState(2);
            List<RefundDetailsDto> list = new ArrayList<>();
            RefundDetailsDto refundDetailsDto = new RefundDetailsDto();
            refundDetailsDto.setState(1);
            refundDetailsDto.setGoodsId(orderDetails.getGoodsId());
            refundDetailsDto.setCount(orderDetails.getXCount() - count);
            refundDetailsDto.setReturnWhy("数量不够");
            list.add(refundDetailsDto);
            if (adminService.addReturnRecord(refundDto,list)) {
                final long orderId = orderDetails.getOrderId();
                //先判断改订单是否开始分拣
                final Sorting sorting = viewSort(orderId);
                if (sorting != null && sorting.getState() == 1){//开始分拣
                    //修改订单细节状态
                    if (orderDetailsService.updateDetailsState(detailsId,1)) {
                        //增加一条分拣细节
                        if (sortMapper.addDetails(orderDetails.getGoodsId(),orderDetails.getSCount(),sorting.getId(),stockId) > 0) {
                            //判断是否要修改分拣订单的状态
                            final List<SortingDetails> sortingDetails = viewDetails(sorting.getId());
                            final List<OrdersVo> ordersVoList = orderService.viewOrder(orderId);
                            if (ordersVoList == null){
                                return false;
                            }
                            final List<OrderDetailsVo> orderDetailsVoList = ordersVoList.get(0).getOrderDetailsVoList();
                            if (orderDetailsVoList.size() == sortingDetails.size()){
                                //需要修改
                                if (sortMapper.updateState(sorting.getId(),2) > 0) {
                                    //分拣订单状态修改成功
                                    //修改订单状态
                                    if (orderService.updateOrderState(orderId,3)) {
                                        return true;
                                    }
                                }
                                return false;
                            }
                            return true;
                        }
                    }
                }else {
                    //分拣订单中没有此订单
                    SortDto sortDto = new SortDto();
                    sortDto.setOrderId(orderId);
                    sortDto.setAdminId(adminId);
                    if (sortMapper.addSort(sortDto) > 0) {
                        //分拣纪录增加成功 修改订单细节状态
                        if (orderDetailsService.updateDetailsState(detailsId,1)) {
                            //增加一条分拣细节
                            if (sortMapper.addDetails(orderDetails.getGoodsId(),orderDetails.getSCount(),sortDto.getId(),stockId) > 0) {
                                //判断是否需要修改订单状态
                                final List<OrdersVo> ordersVoList = orderService.viewOrder(orderId);
                                final List<SortingDetails> sortingDetails = sortMapper.viewDetails(sortDto.getId());
                                if (ordersVoList.isEmpty()){
                                    return false;
                                }
                                final List<OrderDetailsVo> orderDetailsVoList = ordersVoList.get(0).getOrderDetailsVoList();
                                if (orderDetailsVoList.size() == sortingDetails.size()){
                                    //修改订单状态
                                    if (orderService.updateOrderState(orderId,3)) {
                                        //修改分拣订单状态
                                        if (sortMapper.updateState(sortDto.getId(),2) > 0) {
                                            return true;
                                        }
                                    }
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }



        return false;
    }

    /**
     * 查看分拣订单
     * @param orderId
     * @return
     */
    @Override
    public Sorting viewSort(long orderId) {
        return sortMapper.viewSort(orderId);
    }

    /**
     * 根据分拣订单id查看细节
     * @param sortId
     * @return
     */
    @Override
    public List<SortingDetails> viewDetails(long sortId) {
        return sortMapper.viewDetails(sortId);
    }

    /**
     * 查看分拣纪录
     * @param qDate
     * @param hDate
     * @param page
     * @param num
     * @param adminId
     * @param goodsId
     * @return
     */
    @Override
    public List<SortVo> viewSortRecord(Date qDate, Date hDate, Integer page, Integer num, Integer adminId,  Long goodsId) {
        PageHelper.startPage(page,num);
        return sortMapper.viewSortRecord(qDate, hDate,adminId, goodsId);
    }
}
